package com.gccloud.design.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.SdPrincipleAttrDao;
import com.gccloud.design.dao.SdPrincipleDao;
import com.gccloud.design.dto.SSPrincipleSaveDto;
import com.gccloud.design.dto.SSPrincipleUpdateDto;
import com.gccloud.design.entity.SdDispatchPrincipleAttrEntity;
import com.gccloud.design.entity.SdDispatchPrincipleEntity;
import com.gccloud.design.entity.SysDictItemEntity;
import com.gccloud.design.feign.DictClient;
import com.gccloud.design.service.SdPrincipleService;
import com.gccloud.design.vo.BDValueVo;
import com.gccloud.design.vo.PrincipleAttrVO;
import com.gccloud.design.vo.SdPrincipleVO;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import com.gccloud.starter.feign.api.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author mengyu
 * @date 2020/12/26 19:01
 */
@Service
public class SdPrincipleServiceImpl extends ServiceImpl<SdPrincipleDao, SdDispatchPrincipleEntity> implements SdPrincipleService {

    @Resource
    private SdPrincipleDao sdPrincipleDao;

    @Resource
    private DictClient dictClient;

    @Resource
    private SdPrincipleAttrDao sdPrincipleAttrDao;

    @Resource
    private ISysUserService iSysUserService;

    @Override
    public PageVO<SdPrincipleVO> getPrinciple(SearchDTO searchDTO) {
        //1.获取查询条件
        SdDispatchPrincipleEntity sdDispatchPrincipleEntity = JacksonUtils.toObj(searchDTO.getSearchKey(), SdDispatchPrincipleEntity.class);
        // 2.获取分页条件
        Page<SdDispatchPrincipleEntity> searchPage = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());

        IPage<SdPrincipleVO> page = sdPrincipleDao.getPrinciple(sdDispatchPrincipleEntity,searchPage);


        return new PageVO<>(page);
    }

    @Transactional
    @Override
    public void savePrinciple(SSPrincipleSaveDto ssPrincipleSaveDto) {
        //调度原则入库
        SdDispatchPrincipleEntity sdDispatchPrincipleEntity = BeanConvertUtils.convert(ssPrincipleSaveDto, SdDispatchPrincipleEntity.class);
        sdDispatchPrincipleEntity.setCreatorId(iSysUserService.getCurrentUser().getData().getId());
        sdPrincipleDao.savePrinciple(sdDispatchPrincipleEntity);
        //获取原则属性对象,入库
        if(ssPrincipleSaveDto.getPrinAttr() != null && !"".equals(ssPrincipleSaveDto.getPrinAttr())) {
            List<SdDispatchPrincipleAttrEntity> ssPrincipleAttrEntities = JSONArray.parseArray(ssPrincipleSaveDto.getPrinAttr(), SdDispatchPrincipleAttrEntity.class);
            for (SdDispatchPrincipleAttrEntity s:ssPrincipleAttrEntities) {
                s.setPrincipleId(sdDispatchPrincipleEntity.getPrincipleId());
                s.setCreatorId(iSysUserService.getCurrentUser().getData().getId());
                s.setLastModUserid(iSysUserService.getCurrentUser().getData().getId());
                sdPrincipleAttrDao.insert(s);
            }
        }
    }

    @Transactional
    @Override
    public void updatePrinciple(SSPrincipleUpdateDto ssPrincipleUpdateDto) {
        //修改调度原则
        SdDispatchPrincipleEntity sdDispatchPrincipleEntity = BeanConvertUtils.convert(ssPrincipleUpdateDto, SdDispatchPrincipleEntity.class);
        sdDispatchPrincipleEntity.setLastModUserid(iSysUserService.getCurrentUser().getData().getId());
        sdPrincipleDao.updateById(sdDispatchPrincipleEntity);
        //修改原则属性对象
            if(ssPrincipleUpdateDto.getPrinAttr() != null && !"".equals(ssPrincipleUpdateDto.getPrinAttr())) {
                List<SdDispatchPrincipleAttrEntity> sdDispatchPrincipleAttrEntityList = JSONArray.parseArray(ssPrincipleUpdateDto.getPrinAttr(), SdDispatchPrincipleAttrEntity.class);
                for (SdDispatchPrincipleAttrEntity p : sdDispatchPrincipleAttrEntityList) {
                    p.setLastModUserid(iSysUserService.getCurrentUser().getData().getId());
                    QueryWrapper<SdDispatchPrincipleAttrEntity> wrapper = new QueryWrapper<>();
                    wrapper.eq("PRINCIPLE_ID",p.getPrincipleId());
                    sdPrincipleAttrDao.update(p,wrapper);
                }
            }
    }


    @Override
    public void delPrinciple(int principleId) {
        sdPrincipleAttrDao.deleteById(principleId);
        sdPrincipleDao.deleteById(principleId);
    }


    @Override
    public List<BDValueVo> getBDValue(String typeCode) {
        List<BDValueVo> bdValueVoList = new ArrayList<>();
        R<List<SysDictItemEntity>> r = dictClient.getByDictCode(typeCode);

        for (SysDictItemEntity b:r.getData()) {
            BDValueVo bdValueVo = BeanConvertUtils.convert(b, BDValueVo.class);
            bdValueVoList.add(bdValueVo);
        }
        return bdValueVoList;
    }

    @Override
    public List<PrincipleAttrVO> getPrinAttr(int principleId) {
        List<PrincipleAttrVO> principleAttrVOList = new ArrayList<>();
        List<SdDispatchPrincipleAttrEntity> list = sdPrincipleAttrDao.selectList(new QueryWrapper<SdDispatchPrincipleAttrEntity>().eq("PRINCIPLE_ID",principleId));
        for (SdDispatchPrincipleAttrEntity p:list) {
            PrincipleAttrVO principleAttrVO = BeanConvertUtils.convert(p, PrincipleAttrVO.class);
            principleAttrVOList.add(principleAttrVO);
        }
        return principleAttrVOList;
    }

    @Override
    public List<PrincipleAttrVO> getAttr(String dictCode) {
        R<List<SysDictItemEntity>> r = dictClient.getByDictCode(dictCode);
        List<PrincipleAttrVO> list  = new ArrayList<>();
        for (SysDictItemEntity b: r.getData()) {
            PrincipleAttrVO principleAttrVO = new PrincipleAttrVO();
            principleAttrVO.setAttrCode(b.getDictCode());
            principleAttrVO.setAttrName(b.getName());
            list.add(principleAttrVO);
        }
        return list;

    }

    @Override
    public PageVO<SdPrincipleVO> getPrincipleByIds(SearchDTO searchDTO, String ids) {

        SdDispatchPrincipleEntity sdDispatchPrincipleEntity = new SdDispatchPrincipleEntity();
        if(searchDTO.getSearchKey() != null && !"".equals(searchDTO.getSearchKey())) {
            sdDispatchPrincipleEntity = JacksonUtils.toObj(searchDTO.getSearchKey(), SdDispatchPrincipleEntity.class);
         }
        Page<SdDispatchPrincipleEntity> searchPage = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());

        IPage<SdPrincipleVO> page = sdPrincipleDao.getPrincipleByIds(sdDispatchPrincipleEntity, searchPage, ids);

        return new PageVO<>(page);

    }


}
